//
//  HYFAccountTool.m
//  MyOCWeibo
//
//  Created by 黄勇锋 on 16/10/6.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

#import "HYFAccountTool.h"

#define ACCOUNT_PATH  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"account.achieve"]


@implementation HYFAccountTool

+ (void)saveAccount:(HYFAccount *)account{
    
    //归档,保存 account 模型里面的属性
    [NSKeyedArchiver archiveRootObject:account toFile:ACCOUNT_PATH];
}


+ (HYFAccount *)account{
    
    HYFAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:ACCOUNT_PATH];
    
    if (account == nil) {
        return nil;
    }
    //计算过期时间
    NSDate *date = [account.creatDate dateByAddingTimeInterval:account.expires_in];
    //取出当前的时间
    NSDate *currentDate = [NSDate date];
    //如果当前时间大于过期时间(过期了),那么重新进入登录界面.
    if ([currentDate compare:date] != NSOrderedAscending) {
        
        account = nil;
    }
    
    return account;
}

@end
